package cn.com.nes.site.action.newmall;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.UserInfo;
import cn.com.nes.site.module.service.crm.ClueService;
import cn.com.nes.site.module.service.newmall.NewmallBookingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 商城预约
 */
@Controller("newmallBooking")
@RequestMapping("/newmallBooking")
public class NewmallBookingAction {
    @Resource
    private NewmallBookingService service;

    @Resource
    private ClueService service_clue;

    //新增预约信息
    @RequestMapping(method = RequestMethod.POST, value = "/saveBookingInfo")
    public @ResponseBody
    PageData saveBookingInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        String uuid = (String) req.getReqData().get("uuid");
        Map<String, Object> companyInfoMap =new HashMap<>();
        if (StringUtils.isEmpty(uuid)) {
            companyInfoMap = service.getCompanyById(req);
            if (companyInfoMap.size()==0){
                res.setRetCode(9);
                res.setRetMessage("所选门店不存在！");
                return res;
            }
            req.getReqData().put("companyId", companyInfoMap.get("companyId"));
        }
        res = service.saveBookingInfo(req);

        //插入线索
        if(StringUtils.isEmpty(uuid) && res.getRetCode()==0) {
            UserInfo uinfo = new UserInfo();
            uinfo.setId((String)req.getReqData().get("createUid"));
            uinfo.setCompanyId((String) companyInfoMap.get("companyId"));
            req.setUinfo(uinfo);

            req.getReqData().put("uuid",null) ;
            req.getReqData().put("cluestage", "01") ; //线索阶段
            req.getReqData().put("followupstatus", "01") ; //跟进状态
            req.getReqData().put("poolentrymode", "05") ; //入池方式
            req.getReqData().put("cluesource", "29") ;//线索流量来源
            req.getReqData().put("culestate", "02") ; //线索状态，新线索
            req.getReqData().put("cluename", req.getReqData().get("yyr_name")) ;//线索名称
            req.getReqData().put("companycontact",req.getReqData().get("yyr_name")) ;//企业联系人
            req.getReqData().put("cluephone",req.getReqData().get("yyr_phone") ) ;//联系人电话
            req.getReqData().put("pooltype", "其他") ;//企业身份
            req.getReqData().put("personpost", "其他") ;//联系人职务
            req.getReqData().put("businessdirection", "设备") ;//业务方向
            req.getReqData().put("xzqhdm",companyInfoMap.get("areacode")) ;
            req.getReqData().put("longitude",companyInfoMap.get("longitude")) ;
            req.getReqData().put("latitude",companyInfoMap.get("latitude")) ;
            req.getReqData().put("cluebelongtocompany", companyInfoMap.get("companyId")) ;//线索所属公司 利和商城
            req.getReqData().put("customeraddress", req.getReqData().get("yyr_xxdz")) ;//客户所在地
            req.getReqData().put("newshopBookingId", res.getKey()) ;//新村商城预约id
            req.getReqData().put("isLog", "0") ;
            res = service_clue.addClue(req);
        }
        return res;
    }

    //查询预约信息
    @RequestMapping(method = RequestMethod.POST, value = "/getBookingList")
    public @ResponseBody  PageData getBookingList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getBookingList(req) ;
        return res;
    }
}
